#include <bits/stdc++.h>
using namespace std;
using LL = long long;

// #define zerol

#define FOR(i, x, y) for (decay<decltype(y)>::type i = (x), _##i = (y); i < _##i; ++i)
#define FORD(i, x, y) for (decay<decltype(x)>::type i = (x), _##i = (y); i > _##i; --i)
#ifdef zerol
#define dbg(x...) do { cerr << "\033[32;1m" << #x << " -> "; err(x); } while (0)
void err() { cerr << "\033[39;0m" << endl; }
template<template<typename...> class T, typename t, typename... A>
void err(T<t> a, A... x) { for (auto v: a) cerr << v << ' '; err(x...); }
template<typename T, typename... A>
void err(T a, A... x) { cerr << a << ' '; err(x...); }
#else
#define dbg(...)
#endif
int t;

const int N = 1e3+50;

LL C[N][N];


inline int bitcnt(int x) {
	int res = 0;
	while (x) {
		++res;
		x &= (x-1);
	}
	return res;
}

int maxbit(int n) {
	LL x = 1;
	int cnt = 0;
	while (x <= n) {
		x <<= 1;
		++cnt;
	}
	return cnt;
}

LL p2[100];

bool check(vector<pair<int, int>> &ans, int n) {
	assert(ans.size() <= 500);
	set<pair<int, int>> S;
	for (const auto &p : ans) {
		n -= C[p.first][p.second];
		if (S.count(p))
			return false;
		S.insert(p);
		dbg(n, p.first, p.second, C[p.first][p.second]);
	}
	return n == 0;
}

int main() {

	C[0][0] = 1;
	C[1][0] = C[1][1] = 1;
	for (int i=2; i<N; ++i) {
		C[i][0] = C[i][i] = 1;
		for (int j=1; j<i; ++j)
			C[i][j] = C[i-1][j] + C[i-1][j-1];
	}
	p2[0] = 1;
	for (int i=1; i<32; ++i)
		p2[i] = p2[i-1] << 1;
	// puts("AC");
	int t;
	scanf("%d", &t);
	for (int kk=0; kk<t; ++kk) {
		printf("Case #%d:\n", kk+1);
		vector<pair<int, int>> ans;
		int n;
		cin >> n;
		dbg(n);
		int ln = n;
		if (n <= 501) {
			int now = 0;
			if (n == 501) {
				ans.push_back(make_pair(0, 0));
				ans.push_back(make_pair(1, 0));
				ans.push_back(make_pair(2, 0));
				ans.push_back(make_pair(2, 1));
				ans.push_back(make_pair(2, 2));
				n -= 6;
				now = 3;
			}
			dbg(n);
			while (n) {
				--n;
				ans.push_back(make_pair(now, now));
				++now;
			}

			for (const auto &p : ans)
				printf("%d %d\n", p.first+1, p.second+1);
			// assert(check(ans, ln));
			continue;
		} else if (n <= 100) {

			int x = -1;
			for (int i=0; i<32; ++i) {
				if (n-i-p2[i] >=0) {
					x = i;
				}
			}

			for (int i=0; i<x; ++i) {
				ans.push_back(make_pair(i, 0));
			}

			for (int i=0; i<=x; ++i) {
				ans.push_back(make_pair(x, i));
			}
			n = n - x - p2[x];
			int now = x + 1;
			while (n) {
				--n;
				ans.push_back(make_pair(now, now));
				++now;
			}
			for (const auto &p : ans)
				printf("%d %d\n", p.first+1, p.second+1);
			// assert(check(ans, ln));
			continue;
		}	
		

		

		int x = -1;
		dbg("FUCK");
		
		for (int i=0; i<32; ++i) {
			if (n-i-p2[i] >=0) {
				x = i;
			} else
				break;
		}

		for (int i=0; i<x; ++i) {
			ans.push_back(make_pair(i, 0));
		}

		for (int i=0; i<x; ++i) {
			ans.push_back(make_pair(x, i));
		}
		n = n - x - p2[x] + 1;
		dbg(n, x);
		while (n > x-1 && x >= 4) {
			dbg(x);
			--x;
			ans.push_back(make_pair(x, x-1));
			n -= x;

			vector<pair<int, int>> tail;
			for (int j=x-2; j>=1; --j) {
				if (n >= C[x][j] + C[x-1][j]) {
					tail.push_back(make_pair(x-1, j));
					ans.push_back(make_pair(x, j));
					n -= C[x][j] + C[x-1][j];
				} else 
					break;
			}
			dbg(x, tail.size(), n);
			reverse(tail.begin(), tail.end());
			for (const auto &p : tail)
				ans.push_back(p);
			if (tail.size())
				--x;
		}
		dbg(n, x);
		while (n) {
			ans.push_back(make_pair(x, x));
			++x;	
			--n;
		}
		for (const auto &p : ans)
			printf("%d %d\n", p.first+1, p.second+1);
		// assert(check(ans, ln));
		
	}
	
    return 0;
}